<?php
/**
 * Created by PhpStorm.
 * User: yu
 * Date: 2017/1/9
 * Time: 下午4:10
 */

namespace Admin\Controller;


use Common\Controller\AdminController;
use Common\Model\GoodsAttrModel;
use Common\Model\GoodsListModel;
use Common\Model\TypeAttrModel;

class GoodsListController extends AdminController
{
	protected $typeAttrModel;
	protected $goodsAttrModel;
	protected $goodsListModel;
	public function __init(){
		parent::__init();
		$this->typeAttrModel = new TypeAttrModel();
		$this->goodsAttrModel = new GoodsAttrModel();
		$this->goodsListModel = new GoodsListModel();
	}
	public function lists(){
		$gid = I('get.gid');//获得商品id
		$tid = I('get.tid');//获得类型id
		//根据类型id拿到该商品的规格数据
		if(IS_POST){
			$re = $this->goodsListModel->addGoodsList();
			if($re['status']=='success'){
				$this->success($re['message']);
			}else{
				$this->error($re['message']);
			}
			die;
		}

		//获取规格并组合规格选项相关数据
		$specList = $this->typeAttrModel->getSpecList($tid,$gid);

		$this->assign('specList',$specList);

		//获取已有货品
		$old = $this->goodsListModel->where("gid=$gid")->select();

		foreach ($old as $k=>$v){
			$old[$k]['spec'] = $this->goodsAttrModel->where("gtid in (".$v['combine'].")")->getField('gtid,gtvalue');
		}
		$this->assign('old',$old);

		//以下代码用来作为重复添加货品判断依据
		//获得规格数量
		$specNum = count($specList);
		$this->assign('specNum',$specNum);
		$this->display();
	}

	/**
	 * 异步检查货品是否已经存在
	 */
	public function ajaxCheck(){
		$combine = I('post.combine');
		$re = $this->goodsListModel->where("combine='$combine'")->find();
		if($re){
			echo true;die;
		}
		echo false;die;
	}
}